//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.example.administrator.myjdlc.net;

import android.os.SystemClock;

import com.example.administrator.myjdlc.AppApplication;
import com.example.administrator.myjdlc.R;
import com.example.administrator.myjdlc.Thread.CustomThreadPoolExecutor;
import com.example.administrator.myjdlc.bean.ServiceData;
import com.example.administrator.myjdlc.utils.CommonUtils;
import com.example.administrator.myjdlc.utils.JsonValidator;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class NetService {
    private static boolean isRunning = false;
    CustomThreadPoolExecutor exec = new CustomThreadPoolExecutor();
    private static final int CONNECT_TIME_OUT = 60000;
    private static final int READ_TIME_OUT = 10000;
    private static NetService service;
    private int connectTimeOut = '\uea60';
    private int readTimeOut = 10000;
    private String url;
    private String loadUrl;
    private String loadKey;
    private Map<String, String> params = new ConcurrentHashMap();
    private Map<String, Map<String, String>> keyParams = new ConcurrentHashMap();
    private String httpMethod = "POST";
    private ServiceContentHandler serviceContentHandler;
    private BackData backData = new BackData();
    static CommonParameters commonP;
    private boolean threadStart = false;

    public static NetService getInstance() {
        if(service == null) {
            service = new NetService();
        }

        commonP = CommonParameters.getInstance();
        return service;
    }

    public NetService() {
        this.exec.init();
    }

    public void setConnectTimeOut(int connectTimeOut) {
        this.connectTimeOut = connectTimeOut;
    }

    public void setReadTimeOut(int readTimeOut) {
        this.readTimeOut = readTimeOut;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setParams(String key, String value) {
        this.params.put(key, value);
    }

    public void setHttpMethod(String httpMethod) {
        this.httpMethod = httpMethod;
    }

    public void setSrviceContentHandler(ServiceContentHandler serviceContentHandler) {
        this.serviceContentHandler = serviceContentHandler;
    }

    public void clearParams() {
        if(this.params.size() > 0) {
            this.params.clear();
        }

    }

    public void loader(NetLoadListener netLoadListener) {
        this.loader(netLoadListener, (ServiceContentHandler)null);
    }

    public void loader(NetLoadListener netLoadListener, ServiceContentHandler serviceContentHandler) {
        isRunning = true;
        this.serviceContentHandler = serviceContentHandler;
        if(CommonUtils.getAPNType(AppApplication.getContext()) == 0) {
            if(netLoadListener != null) {
                this.backData.setNetResultCode(-1000);
                this.backData.setObject(AppApplication.getContext().getString(R.string.network_failure));
                netLoadListener.onloaderListener(this.backData);
                isRunning = false;
            }
        } else {
            this.params.put("appkey", commonP.getAppKey());
            this.params.put("timestamp", commonP.getTimeStamp());
            this.params.put("source", commonP.getSource());
            this.params.put("eid", commonP.getEid());
            if(commonP.getToken() != null && !commonP.getToken().equals("")) {
                this.params.put("token", commonP.getToken());
            }

            String signStr = SignUtil.createSign(this.params);
            if(!signStr.equals("")) {
                this.params.put("sign", signStr);
            }

            this.keyParams.put(CommonUtils.md5(this.url), this.params);
            this.getDataByNet(netLoadListener, this.url);
        }

    }

    private void getDataByNet(final NetLoadListener netLoadListener, String url) {
        this.loadUrl = new String(url);
        this.loadKey = CommonUtils.md5(url);
        Runnable task = new Runnable() {
            public void run() {
                HttpsURLConnection httpURLConnection = null;
                HashMap list = new HashMap();
                new HashMap();
                String urlValue = NetService.this.loadUrl;
                String urlStr = NetService.this.loadUrl;
                String key = NetService.this.loadKey;
                String params_str = "";
                PrintWriter printWriter = null;

                try {
                    Map paramsMap = (Map)NetService.this.keyParams.get(key);
                    if(paramsMap == null || paramsMap.size() != 0) {
                        URL uRL;
                        Iterator inputStream1;
                        if(NetService.this.httpMethod.equals("GET")) {
                            if(paramsMap != null && !paramsMap.isEmpty()) {
                                inputStream1 = paramsMap.entrySet().iterator();

                                while(inputStream1.hasNext()) {
                                    Entry e1 = (Entry)inputStream1.next();
                                    if(params_str.equals("")) {
                                        params_str = (String)e1.getKey() + "=" + URLEncoder.encode((String)e1.getValue(), "utf-8");
                                    } else {
                                        params_str = params_str + "&" + (String)e1.getKey() + "=" + URLEncoder.encode((String)e1.getValue(), "utf-8");
                                    }
                                }
                            }

                            if(urlValue.contains("?")) {
                                urlValue = urlValue + "&" + params_str;
                            } else {
                                urlValue = urlValue + "?" + params_str;
                            }

                            uRL = new URL(urlValue.trim());

                            try {
                                SSLContext e2 = SSLContext.getInstance("TLS");
                                e2.init((KeyManager[])null, new TrustManager[]{NetService.this.new MyTrustManager()}, new SecureRandom());
                                HttpsURLConnection.setDefaultSSLSocketFactory(e2.getSocketFactory());
                                HttpsURLConnection.setDefaultHostnameVerifier(NetService.this.new MyHostnameVerifier());
                                httpURLConnection = (HttpsURLConnection)uRL.openConnection();
                                httpURLConnection.setRequestMethod("GET");
                            } catch (Exception var19) {
                                var19.printStackTrace();
                            }
                        } else {
                            StringBuffer e = new StringBuffer();
                            if(paramsMap != null && !paramsMap.isEmpty()) {
                                Iterator outbytes = paramsMap.entrySet().iterator();

                                while(outbytes.hasNext()) {
                                    Entry inputStream = (Entry)outbytes.next();
                                    list.put((String)inputStream.getKey(), (String)inputStream.getValue());
                                }
                            }

                            inputStream1 = paramsMap.entrySet().iterator();
                            Entry outbytes1;
                            if(paramsMap.containsKey("Comment")) {
                                while(true) {
                                    while(inputStream1.hasNext()) {
                                        outbytes1 = (Entry)inputStream1.next();
                                        if(!outbytes1.getKey().equals("Comment") && !outbytes1.getKey().equals("Item1") && !outbytes1.getKey().equals("Item2") && !outbytes1.getKey().equals("Item3") && !outbytes1.getKey().equals("Item4")) {
                                            if(params_str.equals("")) {
                                                params_str = outbytes1.getKey() + "=" + URLEncoder.encode(outbytes1.getValue().toString(), "utf-8");
                                            } else {
                                                params_str = params_str + "&" + outbytes1.getKey() + "=" + URLEncoder.encode(outbytes1.getValue().toString(), "utf-8");
                                            }
                                        } else if(e.length() == 0) {
                                            e.append(outbytes1.getKey() + "=" + URLEncoder.encode(outbytes1.getValue().toString(), "utf-8"));
                                        } else {
                                            e.append("&" + outbytes1.getKey() + "=" + URLEncoder.encode(outbytes1.getValue().toString(), "utf-8"));
                                        }
                                    }

                                    e.append("&");
                                    break;
                                }
                            } else {
                                label321:
                                while(true) {
                                    while(true) {
                                        if(!inputStream1.hasNext()) {
                                            break label321;
                                        }

                                        outbytes1 = (Entry)inputStream1.next();
                                        if(!outbytes1.getKey().equals("password") && !outbytes1.getKey().equals("account_info") && !outbytes1.getKey().equals("new_pwd") && !outbytes1.getKey().equals("image")) {
                                            if(params_str.equals("")) {
                                                params_str = outbytes1.getKey() + "=" + URLEncoder.encode(outbytes1.getValue().toString(), "utf-8");
                                            } else {
                                                params_str = params_str + "&" + outbytes1.getKey() + "=" + URLEncoder.encode(outbytes1.getValue().toString(), "utf-8");
                                            }
                                        } else {
                                            e.append("");
                                            e.append("=");
                                            e.append(URLEncoder.encode(String.valueOf(outbytes1.getValue()), "utf-8"));
                                            e.append("&");
                                        }
                                    }
                                }
                            }

                            if(urlValue.contains("?")) {
                                urlValue = urlValue + "&" + params_str;
                            } else {
                                urlValue = urlValue + "?" + params_str;
                            }

                            if(e.length() > 0) {
                                e.deleteCharAt(e.length() - 1);
                            }

                            uRL = new URL(urlValue);

                            try {
                                SSLContext outbytes2 = SSLContext.getInstance("TLS");
                                outbytes2.init((KeyManager[])null, new TrustManager[]{NetService.this.new MyTrustManager()}, new SecureRandom());
                                HttpsURLConnection.setDefaultSSLSocketFactory(outbytes2.getSocketFactory());
                                HttpsURLConnection.setDefaultHostnameVerifier(NetService.this.new MyHostnameVerifier());
                                httpURLConnection = (HttpsURLConnection)uRL.openConnection();
                            } catch (Exception var18) {
                                var18.printStackTrace();
                            }

                            byte[] outbytes3 = e.toString().getBytes("UTF-8");
                            httpURLConnection.setRequestProperty("Content-Length", String.valueOf(outbytes3.length));
                            httpURLConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
                            httpURLConnection.setDoOutput(true);
                            httpURLConnection.setDoInput(true);
                            httpURLConnection.setRequestMethod("POST");
                            printWriter = new PrintWriter(httpURLConnection.getOutputStream());
                            printWriter.write(e.toString());
                            printWriter.flush();
                        }

                        httpURLConnection.setConnectTimeout(NetService.this.connectTimeOut);
                        httpURLConnection.setReadTimeout(10000);
                        httpURLConnection.connect();
                        int e3 = httpURLConnection.getResponseCode();
                        if(e3 >= 200 && e3 < 300) {
                            InputStream inputStream2 = httpURLConnection.getInputStream();
                            if(inputStream2 != null) {
                                NetService.this.analyseStream(inputStream2, netLoadListener, NetService.this.createUrlKey(urlStr, paramsMap));
                            }

                            return;
                        }

                        if(netLoadListener != null) {
                            NetService.this.backData.setNetResultCode(-99);
                            NetService.this.backData.setObject(Integer.valueOf(e3));
                            netLoadListener.onloaderListener(NetService.this.backData);
                        }

                        return;
                    }
                } catch (IOException var20) {
                    if(netLoadListener != null) {
                        NetService.this.backData.setNetResultCode(-99);
                        NetService.this.backData.setObject(var20.getMessage());
                        netLoadListener.onloaderListener(NetService.this.backData);
                    }

                    return;
                } finally {
                    NetService.isRunning = false;
                    if(printWriter != null) {
                        printWriter.close();
                        printWriter = null;
                    }

                }

            }
        };
        ExecutorService pool = this.exec.getCustomThreadPoolExecutor();
        pool.execute(task);
    }

    private void analyseStream(InputStream inputStream, NetLoadListener netLoadListener, String key) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuffer content = new StringBuffer();

        try {
            String lineTxt;
            try {
                while((lineTxt = reader.readLine()) != null) {
                    content.append(lineTxt);
                }
            } catch (IOException var18) {
                var18.printStackTrace();
            }
        } finally {
            try {
                reader.close();
                reader = null;
                inputStream.close();
                inputStream = null;
            } catch (IOException var16) {
                var16.printStackTrace();
            }

        }

        if(content != null && !content.equals("")) {
            JsonValidator jsonValidator = new JsonValidator();
            if(jsonValidator.validate(content.toString())) {
                try {
                    Gson e = (new GsonBuilder()).disableHtmlEscaping().create();
                    ServiceData data = (ServiceData)e.fromJson(content.toString(), ServiceData.class);
                    if(data != null) {
                        this.backData.setNetResultCode(200);
                        this.backData.setObject(content.toString());
                    } else {
                        this.backData.setNetResultCode(-1000);
                        this.backData.setObject("");
                    }
                } catch (Exception var17) {
                    this.backData.setNetResultCode(-1000);
                    this.backData.setObject("");
                }
            } else {
                this.backData.setNetResultCode(-1000);
                this.backData.setObject("");
            }
        } else {
            this.backData.setNetResultCode(-1000);
            this.backData.setObject("");
        }

        if(netLoadListener != null && key.equals(this.createUrlKey(this.loadUrl, this.params))) {
            netLoadListener.onloaderListener(this.backData);
        }

    }

    public void getIsRunning(final CanNetServiceInter inter) {
        if(!this.threadStart) {
            this.threadStart = true;
            (new Thread(new Runnable() {
                public void run() {
                    for(boolean temp = true; temp; SystemClock.sleep(300L)) {
                        if(!NetService.isRunning) {
                            temp = false;
                            inter.canRequst();
                            NetService.this.threadStart = false;
                        }
                    }

                }
            })).start();
        }

    }

    private void jisuanshijian() {
        final Timer time = new Timer();
        TimerTask task = new TimerTask() {
            public void run() {
                if(NetService.isRunning) {
                    NetService.isRunning = false;
                }

                time.cancel();
            }
        };
        time.schedule(task, 15000L);
    }

    public void setRunning(boolean targe) {
        isRunning = targe;
    }

    public String createUrlKey(String url, Map<String, String> params) {
        String result = "";

        try {
            if(params != null && !params.isEmpty()) {
                Iterator var5 = params.entrySet().iterator();

                while(var5.hasNext()) {
                    Entry e = (Entry)var5.next();
                    if(result.equals("")) {
                        result = (String)e.getKey() + "=" + URLEncoder.encode((String)e.getValue(), "utf-8");
                    } else {
                        result = result + "&" + (String)e.getKey() + "=" + URLEncoder.encode((String)e.getValue(), "utf-8");
                    }
                }
            }

            if(result.contains("?")) {
                result = url + "&" + result;
            } else {
                result = url + "?" + result;
            }
        } catch (UnsupportedEncodingException var6) {
            var6.printStackTrace();
        }

        if(result != null) {
            result = CommonUtils.md5(result);
        }

        return result;
    }

    private class MyHostnameVerifier implements HostnameVerifier {
        private MyHostnameVerifier() {
        }

        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }

    private class MyTrustManager implements X509TrustManager {
        private MyTrustManager() {
        }

        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }
}
